# clean_mysql_table.py

import pymysql
from config import MYSQL_CONFIG

def clean_table(table_name):
    try:
        # 建立数据库连接
        conn = pymysql.connect(**MYSQL_CONFIG)
        cursor = conn.cursor()

        print(f"正在连接 MySQL 数据库...")

        # 使用 DELETE 删除数据（可选 TRUNCATE）
        delete_sql = f"DELETE FROM `{table_name}`;"  # 可替换为 TRUNCATE
        # delete_sql = f"TRUNCATE TABLE `{table_name}`;"

        cursor.execute(delete_sql)
        conn.commit()
        print(f"表 `{table_name}` 数据已清空")


    except Exception as e:
        print(f"操作失败: {e}")
        conn.rollback()
    finally:
        try:
            cursor.close()
            conn.close()
        except:
            pass
        print("已关闭 MySQL 数据库连接")


if __name__ == "__main__":
    table_name = "transport_task_final"
    clean_table(table_name)